package com.invengo.library_rfid.analyze.impl;


import static me.goldze.mvvmhabit.utils.constant.Constants.INVENGO_SHELF_V2;

import com.invengo.library_rfid.analyze.ConvertUtil;
import com.invengo.library_rfid.analyze.EPCStrategy;
import com.invengo.library_rfid.analyze._EpcRuleTag;
import com.invengo.library_rfid.analyze.utils.uhf.UHFApi;
import com.invengo.library_rfid.analyze.utils.uhf.formatHelp.CBookEpcCodeEx;
import com.invengo.library_rfid.analyze.utils.uhf.utils.Hex2BytesUtils;

import java.io.Serializable;
import java.util.Arrays;

import me.goldze.mvvmhabit.utils.KLog;

public class DefaultEPCStrategyImpl implements EPCStrategy , Serializable {
    public DefaultEPCStrategyImpl() {
    }

    @Override
    public _EpcRuleTag getRule() {
        return _EpcRuleTag.RULE_DEFAULT;
    }

    @Override
    public String analyzeShelf(String epc) {
        try {
            String barCode = UHFApi.DeserialiseUserData("", epc);
            return barCode;
        } catch (Exception e) {
            return "";
        }
//        return getShelfCodeByEPC(epc, INVENGO_SHELF_V2);
    }

    public static String getShelfCodeByEPC(String epc, int[] SHELF_INTERVAL_V1) {
        String str = ConvertUtil.str16ToStr2(epc).substring(SHELF_INTERVAL_V1[0], SHELF_INTERVAL_V1[1]);
        int[] shelfInterval = Arrays.copyOfRange(SHELF_INTERVAL_V1, 2, SHELF_INTERVAL_V1.length);
        int cursor = 0;
        StringBuilder sb = new StringBuilder();
        for (int i : shelfInterval) {
            String format = "%0" + (i / 8 + 2) + "d";
            String res = String.format(format, Integer.parseInt(str.substring(cursor, i + cursor), 2));
            cursor += i;
            sb.append(res);
        }
        return sb.toString();
    }


    @Override
    public String analyzeBook(String epc) {
        try {
            String barCode = UHFApi.DeserialiseUserData("", epc);
            return barCode;
        } catch (Exception e) {
            return "";
        }

//        CBookEpcCodeEx cBookEpcCodeEx = null;
//        try {
//            cBookEpcCodeEx = new CBookEpcCodeEx(Hex2BytesUtils.convertHexToBytes(epc));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (cBookEpcCodeEx != null) {
//            KLog.e("解析的epc： " + epc + "  解析结果：" + cBookEpcCodeEx.m_szID);
//            return cBookEpcCodeEx.m_szID;
//        } else
//            return "";
    }

//    public static String getBook96(String epc) {
//        if (epc.length() < 20) {
//            throw new IllegalArgumentException("EPC长度不够。。。");
//        }
//        String strEpc2 = ConvertUtil.str16ToStr2(epc);
//        String barcode = strEpc2.substring(35, 80);
//        long parseInt = Long.parseLong(barcode, 2);
//        String substring = strEpc2.substring(90, 94);
//        String str2 = ConvertUtil.str2ToStr16(substring);
//        int libCode = Integer.parseInt(str2, 16);
//        String barCodeStr = epc.substring(8, 20);
//        long barCodeLong = Long.valueOf(barCodeStr, 16);
//        String barCodeBinStr = Long.toBinaryString(barCodeLong);
//        int strLen = barCodeBinStr.length();
//        if (strLen < 48) {
//            while (strLen < 48) {
//                StringBuffer sb = new StringBuffer();
//                sb.append("0").append(barCodeBinStr);// 左补0
//                // sb.append(str).append("0");//右补0
//                barCodeBinStr = sb.toString();
//                strLen = barCodeBinStr.length();
//            }
//        }
//        barCodeBinStr = barCodeBinStr.substring(3);
//        long barCode = Long.valueOf(barCodeBinStr, 2);
//        String bookCode = parseInt + "";
////        if(libCode == 4) {
//        bookCode = CBookIDRule.EPCToBookID(parseInt, libCode);
////        }
//        return bookCode;
//    }


}
